from proxy.get_proxy import get_proxy , delete_proxy
import requests
import time
import random

def get_html(url , use_proxy=False , retry_times=5 ,
            params=None , timeout=5 , time_sleep=0 , method='get'):
    '''
    :param url:
    :param use_proxy: 是否使用代理IP
    :param retry_times: 失败重试次数
    :param params: 构造ip的参数
    :param timeout: 超时设置
    :param time_sleep: 延迟时间
    :param method: 请求方式
    :return:
    '''
    proxies = None
    if use_proxy:
        proxy = get_proxy()
        proxies = {"http": "http://{}".format(proxy)}
    else:
        proxies = None
    while retry_times > 0:
        try:

            html = requests.get(url, timeout=timeout ,params=params , proxies=proxies)
            return html
        except Exception as e:
            print('删除代理IP时发生异常:html--get_html.py--line 6')
            print('异常信息:{}'.format(e))
            time.sleep(random.randint(0,time_sleep))#随机沉睡
            retry_times -= 1
    # 出错5次, 删除代理池中代理
    delete_proxy(proxy)
    return None

response = get_html('https://www.baidu.com/s?wd=python+%E9%9A%8F%E6%9C%BA%E6%95%B4%E6%95%B0&rsv_spt=1&rsv_iqid=0xd105f65400044c3f&issp=1&f=3&rsv_bp=0&rsv_idx=2&ie=utf-8&rqlang=&tn=baiduhome_pg&ch=&rsv_enter=0&inputT=6229')
print(response.text)